using ASP;
using HoWave.Common;
using HoWave.Data.Bll;
using HoWave.Data.Model;
using HoWave.Web;
using HoWave.Web.Shop;
using System;
using System.Data;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
public class _ShoppingCart : Shop, IRequiresSessionState
{
	protected static int m_ProductID = 0;
	protected int m_SalerID;
	protected int m_BuyUserID;
	protected decimal m_Price;
	protected string m_deliverPayType = "";
	public string ProductType = "1";
	private string _buyProductID = "";
	private string _buyProductNum = "1";
	private string _buyProductPrice = "0";
	private string _buyProductStock = "0";
	private Shop_Product _shop_product = null;
	protected UserControls_cmpy_Top Top1;
	protected ScriptManager ScriptManager1;
	protected GridView GridView1;
	protected UpdatePanel UpdatePanel1;
	protected HtmlGenericControl shopcarcontdiv;
	protected Button btn_delete;
	protected Button btn_changeNum;
	protected Button btn_buyagain;
	protected Button btn_next;
	protected UserControls_CMYP_Foot Foot1;
	protected HtmlForm form1;
	protected string buyProductID
	{
		get
		{
			string buyProductID;
			try
			{
				this._buyProductID = base.Request.QueryString["ProductID"].ToString();
				buyProductID = this._buyProductID;
			}
			catch
			{
				buyProductID = this._buyProductID;
			}
			return buyProductID;
		}
	}
	public string buyProductNum
	{
		get
		{
			if (base.Request.QueryString["count"] != null)
			{
				this._buyProductNum = base.Request.QueryString["count"].ToString();
			}
			return this._buyProductNum;
		}
		set
		{
			this._buyProductNum = value;
		}
	}
	public string buyProductPrice
	{
		get
		{
			if (this.Session["shoppingPrice"] != null)
			{
				if (this.Session["shoppingPrice"].ToString() != "")
				{
					this._buyProductPrice = this.Session["shoppingPrice"].ToString();
				}
			}
			return this._buyProductPrice;
		}
		set
		{
			this._buyProductPrice = value;
		}
	}
	public string buyProductStock
	{
		get
		{
			if (this.Session["shoppingStock"] != null)
			{
				if (this.Session["shoppingStock"].ToString() != "")
				{
					this._buyProductStock = this.Session["shoppingStock"].ToString();
				}
			}
			return this._buyProductStock;
		}
		set
		{
			this._buyProductStock = value;
		}
	}
	protected Shop_Product shop_product
	{
		get
		{
			Shop_Product shop_product;
			if (this._shop_product == null)
			{
				this._shop_product = new Shop_Product(this.conn);
				shop_product = this._shop_product;
			}
			else
			{
				shop_product = this._shop_product;
			}
			return shop_product;
		}
	}
	protected DefaultProfile Profile
	{
		get
		{
			return (DefaultProfile)this.Context.Profile;
		}
	}
	protected global_asax ApplicationInstance
	{
		get
		{
			return (global_asax)this.Context.ApplicationInstance;
		}
	}
	protected void Page_PreInit(object sender, EventArgs e)
	{
		try
		{
		}
		catch (Exception ex)
		{
			base.WriteTemErrMsg(ex.Message.ToString());
		}
	}
	public string GetColor(string SColor)
	{
		string result;
		if (SColor == "")
		{
			result = "默认";
		}
		else
		{
			result = SColor;
		}
		return result;
	}
	public string GetSize(string StrSize)
	{
		string result;
		if (StrSize == "")
		{
			result = "默认";
		}
		else
		{
			result = StrSize;
		}
		return result;
	}
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!base.user.CheckUserLogin())
		{
			this.WriteErrMsg("", Language.GetMemberMsg("Common/NotLogin", new string[0]), this.InstallDir + "Passport/Login.aspx?ComeUrl=" + base.Server.UrlEncode(Fetch.CurrentUrl));
		}
		if (!base.IsPostBack)
		{
			this.CheckUserCart();
			if (!string.IsNullOrEmpty(this.buyProductID))
			{
				this.checkInfo();
				this.fillCartAdd();
			}
			this.ListCart();
		}
	}
	private void CheckUserCart()
	{
		DataTable dataTable = BLLInstance.BllShoppingCarts.GetList(" userID=" + base.user.UserID).Tables[0];
		for (int i = 0; i < dataTable.Rows.Count; i++)
		{
			DataRow dataRow = dataTable.Rows[i];
			bool flag = false;
			int num = dataRow["CartID"].ToInt32(new int[0]);
			if (num > 0)
			{
				int proID = dataRow["productID"].ToInt32(new int[0]);
				DataTable product = this.shop_product.GetProduct(proID);
				if (product.Rows.Count > 0)
				{
					DataRow dataRow2 = product.Rows[0];
					string a = dataRow2["EnableSingleSell"].ToString().Trim().ToLower();
					string a2 = dataRow2["EnableSale"].ToString().Trim().ToLower();
					DateTime dateTime = dataRow2["EndDate"].ToDateTime(new DateTime[0]);
					int num2 = dataRow2["Stocks"].ToInt32(new int[0]);
					DateTime now = DateTime.Now;
					if (a == "false" || a == "0" || a2 == "false" || a2 == "0")
					{
						flag = true;
					}
					if (num2 < 1)
					{
						flag = true;
					}
					if (dateTime.Subtract(now).TotalDays <= 0.0)
					{
						flag = true;
					}
					if (flag)
					{
						BLLInstance.BllShoppingCarts.Delete(num);
					}
					else
					{
						DateTime value = dataRow["InputTime"].ToDateTime(new DateTime[0]);
						TimeSpan timeSpan = now.Subtract(value);
						if (timeSpan.TotalDays > 10.0)
						{
							BLLInstance.BllShoppingCarts.Delete(num);
						}
						else
						{
							if (timeSpan.TotalDays >= 1.0 & timeSpan.TotalDays <= 10.0)
							{
								HoWave.Data.Model.ShoppingCarts model = BLLInstance.BllShoppingCarts.GetModel(num);
								model.CartID = num;
								model.productStocks = new int?(num2);
								model.UpdateTime = new DateTime?(DateTime.Now);
								BLLInstance.BllShoppingCarts.Update(model);
							}
						}
					}
				}
			}
		}
	}
	private void checkInfo()
	{
		if (!string.IsNullOrEmpty(this.buyProductID) && this.buyProductID != "0")
		{
			DataTable productByProductID = this.shop_product.GetProductByProductID(this.buyProductID.ToInt32(new int[0]));
			if (productByProductID.Rows.Count > 0)
			{
				DataRow dataRow = productByProductID.Rows[0];
				this.m_SalerID = Convert.ToInt32(dataRow["SalerID"]);
				this.ProductType = dataRow["ProductType"].ToString();
				string a = dataRow["EnableSingleSell"].ToString().Trim().ToLower();
				string a2 = dataRow["EnableSale"].ToString().Trim().ToLower();
				if (a == "false" || a == "0" || a2 == "false" || a2 == "0")
				{
					this.WriteErrMsg("", Language.Get("Shop/Product/EnableSale"), "");
				}
				else
				{
					int num = dataRow["Stocks"].ToInt32(new int[0]);
					if (num < 1)
					{
						this.WriteErrMsg("", Language.Get("Shop/Product/StocksLess"), "");
					}
					else
					{
						DateTime value = Convert.ToDateTime(dataRow["BeginDate"]);
						if (DateTime.Now.Subtract(value).TotalSeconds < 0.0)
						{
							this.WriteErrMsg("", Language.Get("Shop/Product/NotStartSale"), "");
						}
						else
						{
							if (base.user.UserID == this.m_SalerID)
							{
								this.WriteErrMsg("", Language.Get("Shop/Product/BuySelf"), "");
							}
						}
					}
				}
			}
		}
	}
	private int AddShoppingCartModel()
	{
		int result = 0;
		HoWave.Data.Model.ShoppingCarts shoppingCarts = new HoWave.Data.Model.ShoppingCarts();
		shoppingCarts.userID = new int?(base.user.UserID);
		shoppingCarts.productID = new int?(this.buyProductID.ToInt32(new int[0]));
		shoppingCarts.productStocks = new int?(this.buyProductStock.ToInt32(new int[0]));
		shoppingCarts.productPrice = new decimal?(this.buyProductPrice.ToDecimal2());
		shoppingCarts.buyproductNum = new int?(this.buyProductNum.ToInt32(new int[0]));
		shoppingCarts.productMoney = new decimal?(this.buyProductPrice.ToDecimal2() * this.buyProductNum.ToInt32(new int[0]));
		shoppingCarts.InputTime = new DateTime?(DateTime.Now);
		BLLInstance.BllShoppingCarts.Add(shoppingCarts);
		return result;
	}
	private void fillCartAdd()
	{
		if (!string.IsNullOrEmpty(this.buyProductID))
		{
			string strWhere = string.Concat(new object[]
			{
				" userID=",
				base.user.UserID,
				" and productID=",
				this.buyProductID
			});
			DataTable dataTable = BLLInstance.BllShoppingCarts.GetList(strWhere).Tables[0];
			if (dataTable.Rows.Count > 0)
			{
				DataRow dataRow = dataTable.Rows[0];
				int num = dataRow["CartID"].ToInt32(new int[0]);
				if (num > 0)
				{
					HoWave.Data.Model.ShoppingCarts model = BLLInstance.BllShoppingCarts.GetModel(num);
					if (model != null)
					{
						int value = model.buyproductNum.ToInt32(new int[0]) + this.buyProductNum.ToInt32(new int[0]);
						model.CartID = num;
						model.buyproductNum = new int?(value);
						model.productStocks = new int?(this.buyProductStock.ToInt32(new int[0]));
						model.productPrice = new decimal?(this.buyProductPrice.ToDecimal2());
						model.productMoney = new decimal?(this.buyProductPrice.ToDecimal2() * value);
						BLLInstance.BllShoppingCarts.Update(model);
					}
				}
			}
			else
			{
				this.AddShoppingCartModel();
			}
		}
	}
	private void BindListCart()
	{
		string text = "cart.CartID,cart.userID,cart.productID,cart.productStocks,cart.productPrice,cart.buyproductNum,cart.productMoney as total,cart.InputTime,shop.ShopID as shopID,shop.ShopName as shopName,p.ProductName,p.ProductSmallImageUrl as productPic,p.ShoppingCarGuige,p.ColorAttribute,p.SizeAttribute";
		string sql = string.Concat(new object[]
		{
			"select ",
			text,
			" from HLShop_ShoppingCarts as cart,HLShop_Product as p,HLShop_Shop as shop where cart.productID=p.ProductID and p.ShopID=shop.ShopID and cart.userID=",
			base.user.UserID
		});
		DataSet dataSet = this.conn.GetDataSet(sql);
		this.GridView1.DataSource = dataSet;
		this.GridView1.DataBind();
	}
	private void PartProduct()
	{
		for (int i = 0; i < this.GridView1.Rows.Count; i++)
		{
			int partID = int.Parse(((HiddenField)this.GridView1.Rows[i].FindControl("productID")).Value);
			if (this.shop_product.IfJustPart(partID))
			{
				DataTable partProduct = this.shop_product.GetPartProduct(partID);
				TextBox textBox = (TextBox)this.GridView1.Rows[i].FindControl("txt_productNum");
				if (partProduct != null)
				{
					this.GridView1.Rows[i].Cells[7].Text = (decimal.Parse(partProduct.Rows[0]["yikouPrice"].ToString()) * int.Parse(partProduct.Rows[0]["SecondaryCount"].ToString())).ToString();
					this.GridView1.Rows[i].Cells[5].Text = partProduct.Rows[0]["yikouPrice"].ToString();
					textBox.Text = partProduct.Rows[0]["SecondaryCount"].ToString();
				}
				((CheckBox)this.GridView1.Rows[i].FindControl("cb_select")).Enabled = false;
				textBox.Enabled = false;
			}
			else
			{
				((CheckBox)this.GridView1.Rows[i].FindControl("cb_select")).Enabled = true;
				((TextBox)this.GridView1.Rows[i].FindControl("txt_productNum")).Enabled = true;
			}
		}
	}
	private void Allselect()
	{
		if (this.GridView1.Rows.Count <= 0)
		{
			this.btn_next.Enabled = false;
			this.btn_delete.Enabled = false;
			this.btn_changeNum.Enabled = false;
		}
		else
		{
			this.btn_next.Enabled = true;
			this.btn_delete.Enabled = true;
			this.btn_changeNum.Enabled = true;
			for (int i = 0; i < this.GridView1.Rows.Count; i++)
			{
				CheckBox checkBox = (CheckBox)this.GridView1.Rows[i].FindControl("cb_select");
				checkBox.Checked = true;
			}
			((CheckBox)this.GridView1.HeaderRow.FindControl("cb_selectAll")).Checked = true;
		}
	}
	private void ListCart()
	{
		this.BindListCart();
		this.Allselect();
		this.PartProduct();
	}
	protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
	{
		this.GridView1.EditIndex = e.NewEditIndex;
	}
	protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
	{
		this.GridView1.EditIndex = -1;
	}
	protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
	{
		this.GridView1.EditIndex = -1;
	}
	protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
	{
		if (e.Row.RowType == DataControlRowType.DataRow)
		{
			Label label = (Label)e.Row.FindControl("Label3");
			Label label2 = (Label)e.Row.FindControl("Label2");
			HiddenField hiddenField = (HiddenField)e.Row.FindControl("hfStocks");
			TextBox textBox = (TextBox)e.Row.FindControl("txt_productNum");
			textBox.Attributes.Add("onblur", string.Concat(new string[]
			{
				"WriteBuyNumber(this,'",
				hiddenField.Value,
				"','",
				label.Text,
				"','",
				label2.ClientID,
				"')"
			}));
			HtmlImage htmlImage = (HtmlImage)e.Row.FindControl("decrease");
			htmlImage.Attributes.Add("onclick", string.Concat(new string[]
			{
				"ChangeBuyNumber(",
				textBox.ClientID,
				",'remove','",
				hiddenField.Value,
				"','",
				label.Text,
				"','",
				label2.ClientID,
				"')"
			}));
			HtmlImage htmlImage2 = (HtmlImage)e.Row.FindControl("increase");
			htmlImage2.Attributes.Add("onclick", string.Concat(new string[]
			{
				"ChangeBuyNumber(",
				textBox.ClientID,
				",'add','",
				hiddenField.Value,
				"','",
				label.Text,
				"','",
				label2.ClientID,
				"')"
			}));
		}
	}
	protected void cb_selectAll_CheckedChanged(object sender, EventArgs e)
	{
		CheckBox checkBox = (CheckBox)this.GridView1.HeaderRow.FindControl("cb_selectAll");
		if (checkBox.Checked)
		{
			foreach (GridViewRow gridViewRow in this.GridView1.Rows)
			{
				CheckBox checkBox2 = (CheckBox)gridViewRow.FindControl("cb_select");
				checkBox2.Checked = true;
			}
		}
		else
		{
			foreach (GridViewRow gridViewRow in this.GridView1.Rows)
			{
				CheckBox checkBox2 = (CheckBox)gridViewRow.FindControl("cb_select");
				checkBox2.Checked = false;
			}
		}
	}
	private string GetSelectIDStr()
	{
		string text = "";
		foreach (GridViewRow gridViewRow in this.GridView1.Rows)
		{
			CheckBox checkBox = (CheckBox)gridViewRow.Cells[0].FindControl("cb_select");
			HiddenField hiddenField = (HiddenField)gridViewRow.FindControl("hfCartID");
			if (checkBox.Checked)
			{
				text = text + hiddenField.Value.ToString() + ",";
			}
		}
		return text;
	}
	private void GetSelectIDNumStr(ref string id_array, ref string num_array)
	{
		foreach (GridViewRow gridViewRow in this.GridView1.Rows)
		{
			CheckBox checkBox = (CheckBox)gridViewRow.Cells[0].FindControl("cb_select");
			HiddenField hiddenField = (HiddenField)gridViewRow.FindControl("hfCartID");
			TextBox textBox = (TextBox)gridViewRow.FindControl("txt_productNum");
			if (checkBox.Checked)
			{
				id_array = id_array + hiddenField.Value.ToString() + ",";
				string text = num_array;
				num_array = string.Concat(new string[]
				{
					text,
					hiddenField.Value.ToString(),
					",",
					textBox.Text.ToString(),
					"|"
				});
			}
		}
	}
	protected void btn_delete_Click(object sender, EventArgs e)
	{
		string selectIDStr = this.GetSelectIDStr();
		if (!string.IsNullOrEmpty(selectIDStr))
		{
			string str = selectIDStr.Substring(0, selectIDStr.Length - 1);
			DataTable dtInfo = BLLInstance.BllShoppingCarts.GetList(" CartID in (" + str + ")").Tables[0];
			this.DeleteCartInfo(dtInfo);
			this.ListCart();
		}
		else
		{
			this.WriteErrMsg("", "未选中购物车中的商品项，请您选择要删除的商品项！", "");
		}
	}
	private void DeleteCartInfo(DataTable dtInfo)
	{
		for (int i = 0; i < dtInfo.Rows.Count; i++)
		{
			DataRow dataRow = dtInfo.Rows[i];
			int num = dataRow["CartID"].ToInt32(new int[0]);
			if (num > 0)
			{
				BLLInstance.BllShoppingCarts.Delete(num);
			}
		}
	}
	private static void EditNumByCartID(string num_array, int cartID)
	{
		if (cartID > 0)
		{
			string[] array = num_array.Split(new char[]
			{
				'|'
			});
			string[] array2 = array;
			for (int i = 0; i < array2.Length; i++)
			{
				string text = array2[i];
				string[] array3 = text.Split(new char[]
				{
					','
				});
				if (array3.Length >= 2)
				{
					if (array3[0].ToInt32(new int[0]) == cartID)
					{
						int value = array3[1].ToInt32(new int[0]);
						HoWave.Data.Model.ShoppingCarts model = BLLInstance.BllShoppingCarts.GetModel(cartID);
						if (model != null)
						{
							model.CartID = cartID;
							model.buyproductNum = new int?(value);
							decimal d = model.productPrice.ToDecimal2();
							model.productMoney = new decimal?(d * value);
							BLLInstance.BllShoppingCarts.Update(model);
						}
						break;
					}
				}
			}
		}
	}
	protected void btn_changeNum_Click(object sender, EventArgs e)
	{
		string text = "";
		string text2 = "";
		this.GetSelectIDNumStr(ref text, ref text2);
		if (!string.IsNullOrEmpty(text))
		{
			string str = text.Substring(0, text.Length - 1);
			text2 = text2.Substring(0, text2.Length - 1);
			DataTable dataTable = BLLInstance.BllShoppingCarts.GetList(" CartID in (" + str + ")").Tables[0];
			for (int i = 0; i < dataTable.Rows.Count; i++)
			{
				DataRow dataRow = dataTable.Rows[i];
				int cartID = dataRow["CartID"].ToInt32(new int[0]);
				_ShoppingCart.EditNumByCartID(text2, cartID);
			}
			this.ListCart();
		}
		else
		{
			this.WriteErrMsg("", "未选中购物车中的商品项，请您选择要修改的商品项！", "");
		}
	}
	protected void btn_buyagain_Click1(object sender, EventArgs e)
	{
		base.Response.Redirect("ProductList.aspx?Type=market");
	}
	protected void btn_next_Click1(object sender, EventArgs e)
	{
		if (this.GridView1.Rows.Count <= 0)
		{
			this.Page.ClientScript.RegisterStartupScript(base.GetType(), "Test", "<script>alert('请先购物!');</script>");
		}
		else
		{
			string text = "";
			string text2 = "";
			this.GetSelectIDNumStr(ref text, ref text2);
			string text3 = "";
			if (!string.IsNullOrEmpty(text))
			{
				string str = text.Substring(0, text.Length - 1);
				text2 = text2.Substring(0, text2.Length - 1);
				DataTable dataTable = BLLInstance.BllShoppingCarts.GetList(" CartID in (" + str + ")").Tables[0];
				for (int i = 0; i < dataTable.Rows.Count; i++)
				{
					DataRow dataRow = dataTable.Rows[i];
					int cartID = dataRow["CartID"].ToInt32(new int[0]);
					text3 = text3 + cartID.ToString() + ",";
					_ShoppingCart.EditNumByCartID(text2, cartID);
				}
			}
			else
			{
				this.WriteErrMsg("", "未选中购物车中的商品项，请您选择要购买的商品项！", "");
			}
			if (!string.IsNullOrEmpty(text3))
			{
				text3 = text3.Substring(0, text3.Length - 1);
			}
			base.Response.Redirect("setSendAndPayment.aspx?shopcartID=" + text3);
		}
	}
}
